<?php

namespace App\Tables;

use Illuminate\Database\Eloquent\Model;

class UserBalanceTransferRecord extends Model
{
    protected $table = 'user_balance_transfer_records';

    protected $appends = [
        'type_text',
        'username',
        'region_text'
    ];

    /**
     * 获取类型名称
     * @return mixed|string
     */
    public function getTypeTextAttribute()
    {
        switch ($this->type) {
            case 'consume':
                return '消费';
            case 'charge':
                return '充值';
            case 'refund':
                return '提现';
            case 'withdraw':
                return '提现';
            default:
                return $this->type;
        }
    }

    /**
     * 获取用户信息
     * @return string
     */
    public function getUsernameAttribute()
    {
        $username = $this->user->name;
        if (!empty($this->user->nickname)) {
            $username .= '/' . $this->user->nickname;
        }

        return $username;
    }

    /**
     * 获取用户区域位置
     */
    public function getRegionTextAttribute()
    {
        $regions = '';

        if (isset($this->user->province->name)) {
            $regions .= $this->user->province->name;
        }
        if (isset($this->user->city->name)) {
            $regions .= $this->user->city->name;
        }
        if (isset($this->user->district->name)) {
            $regions .= $this->user->district->name;
        }
        return $regions;
    }

    /**
     * 获取用户详情
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function user()
    {
        return $this->hasOne('App\Tables\User', 'id', 'user_id');
    }
}
